﻿using System.IO;
using System.Web.UI.WebControls;

namespace Sex
{
    public partial class Sex : System.Web.UI.Page
    {
        protected void MenuFileExecute(E_ObjectCommand sf)
        {
            Div_Details.Controls.Add(new ListFileDirectory(MyState).GetData());
            Img img = null;
            Menu_File.Visible = true;
            SetSelectedValue(Menu_File, sf);
            switch (sf)
            {
                case E_ObjectCommand.FRAME:
                    iframe_Content.Visible = true;
                    Literal_Content.Visible = false;
                    Literal_Content.Text = "";
                    break;
                case E_ObjectCommand.NEWWIN:
                    iframe_Content.Visible = false;
                    Literal_Content.Visible = true;
                    Literal_Content.Text = "";
                    break;
                default:
                case E_ObjectCommand.INLINE:
                case E_ObjectCommand.EMBED:
                case E_ObjectCommand.CODE:
                case E_ObjectCommand.HEX:
                    iframe_Content.Visible = false;
                    iframe_Content.Attributes["src"] = "";
                    Literal_Content.Visible = true;
                    Literal_Content.Text = "";
                    break;
                case E_ObjectCommand.SPECIAL:
                case E_ObjectCommand.EXIF:
                case E_ObjectCommand.PERMISSIONS:
                    iframe_Content.Visible = false;
                    iframe_Content.Attributes["src"] = "";
                    Literal_Content.Visible = false;
                    Literal_Content.Text = "";
                    break;
                case E_ObjectCommand.DOWNLOAD:
                    // do no change current display
                    break;
            }
            switch (sf)
            {
                case E_ObjectCommand.FRAME:
                    iframe_Content.Attributes["src"] = Tree.GetUrl(Tree_Content.SelectedNode, MyState.RelativePathComponents, MyState.ApplicationPath);
                    // iframe_Content.Attributes["src"] = "ExeGetFileDirectory.aspx" + "?p=" + Server.UrlEncode(MyState.CurrentPathAbs); // MyState.CurrentPathRel;
                    break;
                case E_ObjectCommand.NEWWIN:
                    Literal_Content.Text = MediaTemplates.Get(14).InstantiateTemplate("", "", Tree.GetUrl(Tree_Content.SelectedNode, MyState.RelativePathComponents, MyState.ApplicationPath)); 
                    break;
                case E_ObjectCommand.INLINE:
                    Literal_Content.Text = File.ReadAllText(MyState.RootDirFullName + MyState.CurrentPathRel);
                    break;
                case E_ObjectCommand.EMBED:
                    int my_x = 0;
                    int my_y = 0;
                    switch (MyState.CurrentType)
                    {
                        case FileGroup.Type.EXIF:
                        case FileGroup.Type.IMAGE:
                            img = new Img(MyState.CurrentPathAbs);
                            int x = img.x;
                            int y = img.y;
                            // Scale Picture to an area 200x200 px retaining aspect ratio
                            int MaxDim = 320;
                            if (x > y) // width>height
                            {
                                my_x = MaxDim;
                                my_y = (int)(MaxDim * ((y * 1.0) / x));
                            }
                            else
                            {
                                my_y = MaxDim;
                                my_x = (int)(MaxDim * ((x * 1.0) / y));
                            }
                            break;
                        case FileGroup.Type.AUDIO:
                        case FileGroup.Type.VIDEO:
                        case FileGroup.Type.AUDVID:
                            my_x = 320;
                            my_y = 200;
                            break;
                        default:
                        case FileGroup.Type.APPLI:
                        case FileGroup.Type.CODEEXE:
                        case FileGroup.Type.BINARY:
                        case FileGroup.Type.APPLIEXE:
                        case FileGroup.Type.OTHER:
                        case FileGroup.Type.NOTYPE:
                            // Literal_Content.Text =
                            //     "<table width='100%' height='100%'><tr><td align='center' valign='middle'>"
                            //     + Literal_Content.Text
                            //     + "</td></tr></table>";
                            break;
                    }
                    MediaTemplate mt = MediaTemplates.Get(int.Parse(DropDownList_Template.SelectedValue));
                    string Template = mt.InstantiateTemplate(my_x.ToString(), my_y.ToString(), Tree.GetPathReduced(Tree_Content.SelectedNode, MyState.RelativePathComponents, MyState.ApplicationPath)); // MyState.CurrentPathRel
                    Literal_Content.Visible = true;
                    Literal_Content.Text = GetHighlightCodeAndInsertScript(MyState, Template, "xml") + Template;
                    break;
                case E_ObjectCommand.CODE:
                    Literal_Content.Text = Untag(File.ReadAllText(MyState.RootDirFullName + MyState.CurrentPathRel));
                    string Brush = MyState.CurrentBrush;
                    if (Brush == "") Brush = "plain";
                    Literal_Content.Text = GetHighlightCodeAndInsertScript(MyState, Literal_Content.Text, Brush);
                    break;
                case E_ObjectCommand.HEX:
                default:
                    Literal_Content.Text = "<pre>" + Hex.Dump(MyState.RootDirFullName + MyState.CurrentPathRel) + "</pre>";
                    break;
                case E_ObjectCommand.PERMISSIONS:
                    Div_ContentItem.Controls.Add(new ListPermissions(MyState).GetData());
                    break;
                case E_ObjectCommand.DOWNLOAD:
                    MenuDownload(MyState.CurrentPathAbs);
                    break;
                case E_ObjectCommand.SPECIAL:
                    switch (MyState.CurrentType)
                    {
                        case FileGroup.Type.EXIF:
                        case FileGroup.Type.IMAGE:
                            if (img == null) img = new Img(MyState.CurrentPathAbs);
                            if (img != null) Div_ContentItem.Controls.Add((new ListImage(MyState, img)).GetData());
                            break;
                    }
                    break;
                case E_ObjectCommand.EXIF:
                    Div_ContentItem.Controls.Add((new ListExif(MyState)).GetData());
                    break;
            }
            MyState.SaveState(ViewState);
        }
        protected void MenuDirectoryExecute(E_ObjectCommand sf)
        {
            Div_Details.Controls.Add(new ListFileDirectory(MyState, Tree.Size(Tree_Content.SelectedNode)).GetData());
            Menu_Directory.Visible = true;
            SetSelectedValue(Menu_Directory, sf);
            switch (sf)
            {
                case E_ObjectCommand.FRAME:
                    Literal_Content.Visible = false;
                    Literal_Content.Text = "";
                    iframe_Content.Visible = true;
                    iframe_Content.Attributes["src"] = Tree.GetUrl(Tree_Content.SelectedNode, MyState.RelativePathComponents, MyState.ApplicationPath);
                    // iframe_Content.Attributes["src"] = "ExeGetFileDirectory.aspx" + "?p=" + Server.UrlEncode(MyState.CurrentPathAbs); // MyState.CurrentPathRel;
                    break;
                case E_ObjectCommand.DIRECTORY:
                    Literal_Content.Visible = true;
                    iframe_Content.Visible = false;
                    iframe_Content.Attributes["src"] = "";
                    Literal_Content.Text = "";
                    Literal_Content.Visible = false;
                    Div_ContentItem.Controls.Add(new ListFileDirectory(MyState, Tree_Content.SelectedNode).GetData());
                    break;
                case E_ObjectCommand.PERMISSIONS:
                    iframe_Content.Visible = false;
                    iframe_Content.Attributes["src"] = "";
                    Literal_Content.Visible = false;
                    Literal_Content.Text = "";
                    Div_ContentItem.Controls.Add(new ListPermissions(MyState).GetData());
                    break;
                case E_ObjectCommand.THUMBS:
                    iframe_Content.Visible = false;
                    iframe_Content.Attributes["src"] = "";
                    Literal_Content.Visible = false;
                    Literal_Content.Text = "";
                    string CurrentPathRel = Tree.GetPathReduced(Tree_Content.SelectedNode, MyState.RelativePathComponents, MyState.ApplicationPath);
                    Div_ContentItem.Controls.Add(new ListThumbs(MyState, CurrentPathRel).GetThumbs(MyState.ThumbDimension, MyState.ThumbCols));
                    break;
            }
            MyState.SaveState(ViewState);
        }
        protected void SetSelectedValue(Menu m, E_ObjectCommand sf)
        {
            for (int i = 0; i < m.Items.Count; i++)
            {
                switch (sf)
                {
                    case E_ObjectCommand.CODE:
                        if (m.Items[i].Value == "FileCode") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.DIRECTORY:
                        if (m.Items[i].Value == "DirectoryDetails") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.DOWNLOAD:
                        if (m.Items[i].Value == "FileDownload") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.SPECIAL:
                        if (m.Items[i].Value == "FileSpecial") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.EMBED:
                        if (m.Items[i].Value == "FileEmbed") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.FRAME:
                        if (m.Items[i].Value == "DirectoryFrame") { m.Items[i].Selected = true; return; }
                        if (m.Items[i].Value == "FileFrame") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.HEX:
                        if (m.Items[i].Value == "FileHex") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.INLINE:
                        if (m.Items[i].Value == "FileInline") { m.Items[i].Selected = true; return; }
                        break;
                    case E_ObjectCommand.PERMISSIONS:
                        if (m.Items[i].Value == "DirectoryPermissions") { m.Items[i].Selected = true; return; }
                        if (m.Items[i].Value == "FilePermissions") { m.Items[i].Selected = true; return; }
                        break;
                }
            }
        }
    }
}
